﻿Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Web

Public Class SaleModifyJGBGSP
    Inherits BusinessDomainBase

    Private _businessGUID As String     '销售变更申请GUID
    Private _saleModiSP As SaleModiSP   '销售变更审批
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/slxt/SaleModifyJGBGSP.xml")
        Me._xml.LoadXml(MyBase.XML)
        Me._saleModiSP = New SaleModiSP()
    End Sub

    '属性：销售变更申请GUID
    Public Overrides Property BusinessGUID() As String
        Get
            Return Me._businessGUID
        End Get
        Set(ByVal Value As String)
            Me._businessGUID = Value
            Me._saleModiSP.BusinessGUID = Me._businessGUID
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return Me._xml.OuterXml
        End Get
        Set(ByVal Value As String)
            Me._xml.LoadXml(Value)
        End Set
    End Property

    '功能：取销售变更申请数据，填充XML
    Public Overrides Function LoadData() As Integer

        Dim strSQL As String
        Dim i, j As Integer
        Dim dtTemp As DataTable

        '主记录    
        strSQL = "SELECT SaleModiApplyGUID, SaleGUID, SaleType, ApplyType, ApplyState, ApproveState, BUGUID, ProjGUID, " & _
                 " RoomGUID, RoomInfo, CstGUID, CstName, BldArea, TnArea, CalMode, Price, TnPrice, FsTotal, PayformName, DiscntValue, " & _
                 " BldCjPrice, TnCjPrice, CjTotal, ReasonSort, Reason, ApplyBy, CONVERT(varchar(10), ApplyDate, 120) as ApplyDate, HandCharge, Pay, Total, " & _
                 " CalModeAfter, PayformNameAfter, DiscntValueAfter, BldCjPriceAfter, TnCjPriceAfter, RoomTotal, " & _
                 " DiscntRemark, ZxBz, ZxPrice, IsZxkbrht, ZxTotal, Bz, ExRate, RmbCjTotalAfter, CjTotalAfter " & _
                 " FROM vs_SaleModiApply_WF " & _
                 " WHERE SaleModiApplyGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then

            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID

            '主记录
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售变更申请GUID']").InnerText = dtTemp.Rows(0).Item("SaleModiApplyGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售单GUID']").InnerText = dtTemp.Rows(0).Item("SaleGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售单类型']").InnerText = dtTemp.Rows(0).Item("SaleType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请类型']").InnerText = dtTemp.Rows(0).Item("ApplyType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请单状态']").InnerText = dtTemp.Rows(0).Item("ApplyState").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='审批状态']").InnerText = dtTemp.Rows(0).Item("ApproveState").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='单位GUID']").InnerText = dtTemp.Rows(0).Item("BUGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='项目GUID']").InnerText = dtTemp.Rows(0).Item("ProjGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间GUID']").InnerText = dtTemp.Rows(0).Item("RoomGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间信息']").InnerText = dtTemp.Rows(0).Item("RoomInfo").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户GUID']").InnerText = dtTemp.Rows(0).Item("CstGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户名称']").InnerText = dtTemp.Rows(0).Item("CstName").ToString

            Dim cstGUIDList As String() = dtTemp.Rows(0).Item("CstGUID").ToString().Split(";")
            Dim cstNameList As String() = dtTemp.Rows(0).Item("CstName").ToString().Split(";")

            If cstGUIDList.Count < 5 Then

                For i = 0 To cstGUIDList.Count - 1

                    j = i + 1
                    _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户GUID" & j.ToString() & "']").InnerText = cstGUIDList(i)
                    _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户名称" & j.ToString() & "']").InnerText = cstNameList(i)

                Next

            End If

            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='建筑面积']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("BldArea")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='套内面积']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("TnArea")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='计价方式']").InnerText = dtTemp.Rows(0).Item("CalMode").ToString()
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='建筑单价']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("Price")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='套内单价']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("TnPrice")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='附属房产价格']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("FsTotal")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='标准总价']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("Total")).ToString("0.00")

            '变更前信息
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='付款方式']").InnerText = dtTemp.Rows(0).Item("PayformName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='折扣']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("DiscntValue")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='建筑成交单价']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("BldCjPrice")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='套内成交单价']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("TnCjPrice")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='协议总价']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("CjTotal")).ToString("0.00")

            '变更后信息
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='计价方式_变更后']").InnerText = dtTemp.Rows(0).Item("CalModeAfter").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='付款方式_变更后']").InnerText = dtTemp.Rows(0).Item("PayformNameAfter").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='折扣_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("DiscntValueAfter")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='建筑成交单价_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("BldCjPriceAfter")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='套内成交单价_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("TnCjPriceAfter")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间总价_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("RoomTotal")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='折扣说明_变更后']").InnerText = dtTemp.Rows(0).Item("DiscntRemark").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='装修标准_变更后']").InnerText = dtTemp.Rows(0).Item("ZxBz").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='装修单价_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("ZxPrice")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='装修是否并入合同_变更后']").InnerText = IIf(dtTemp.Rows(0).Item("IsZxkbrht").ToString() = 1, "是", "否")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='装修总价_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("ZxTotal")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='币种_变更后']").InnerText = dtTemp.Rows(0).Item("Bz").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='汇率_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("ExRate")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='协议总价_人民币_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("RmbCjTotalAfter")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='协议总价_变更后']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("CjTotalAfter")).ToString("0.00")

            '变更信息
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原因分类']").InnerText = dtTemp.Rows(0).Item("ReasonSort").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原因说明']").InnerText = dtTemp.Rows(0).Item("Reason").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请人']").InnerText = dtTemp.Rows(0).Item("ApplyBy").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请日期']").InnerText = dtTemp.Rows(0).Item("ApplyDate").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='手续费']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("HandCharge")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='已交金额']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("Pay")).ToString("0.00")

        End If

        Return 0

    End Function

    '功能：发起“销售变更”的审批流程
    Public Overrides Function Initiate() As String

        Return Me._saleModiSP.Initiate()

    End Function

    '功能：根据XML进行数据对“销售变更”进行归档
    Public Overrides Function SaveData() As String

        Return Me._saleModiSP.SaveData()

    End Function

    '功能：不通过处理
    Public Overrides Function SaveData_NoPass() As String

        Return Me._saleModiSP.SaveData_NoPass()

    End Function

    '功能：审批作废处理
    Public Overrides Function SaveData_Cancel() As String

        Return Me._saleModiSP.SaveData_Cancel()

    End Function

    '功能：发起撤回“销售变更”的审批流程
    Public Overrides Function InitiateWithDrawn() As String

        '由于发起撤回的审批流程和审批作废处理是一样的，所以直接调用审批作废的方法即可
        Return Me._saleModiSP.InitiateWithDrawn()

    End Function

End Class
